//
//  YDUserAccountModel.swift
//  YDWeibo01
//
//  Created by suibian on 16/8/10.
//  Copyright © 2016年 iitcast. All rights reserved.
//

import UIKit
//使用归档和解当需要继承NSCoding

class YDUserAccountModel: NSObject,NSCoding {
//    用户的名字信息
    var screen_name: String?
//    用户的头像
    var avatar_large:String?
    
    //    过期时间 (程序员自己加字段)
    var expire_Date:NSDate?
    
//    用户授权唯一票据
    var access_token:String?
//    access_token的生命周期 单位是秒
    var expires_in:NSTimeInterval = 0{
    
        didSet{
            expire_Date = NSDate(timeIntervalSinceNow: expires_in)
        
        }
    }

//    归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
         aCoder.encodeObject(expire_Date, forKey: "expire_Date")
         aCoder.encodeObject(uid, forKey: "uid")
         aCoder.encodeObject(avatar_large, forKey: "avatar_large")
         aCoder.encodeObject(screen_name, forKey: "screen_name")
    }
//    解当
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expire_Date = aDecoder.decodeObjectForKey("expire_Date") as? NSDate

        uid = aDecoder.decodeObjectForKey("uid") as? String

        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String

        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String

    }
    
//    授权用户的UID
    var uid:String?
    
    init(dict:[String:AnyObject]){
    
    super.init()
        setValuesForKeysWithDictionary(dict)
    }
//    服务器加字段 防止程序崩溃
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
//    要打印当前类的属性要重写description
    override var description:String{
    
        let keys = ["access_token", "expire_Date", "uid","avatar_large","screen_name"]
        return dictionaryWithValuesForKeys(keys).description

    }
}
